package com.lavegue.webaction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.lavegue.common.BaseAction;

import com.lavegue.model.Area;
import com.lavegue.model.BisGroup;
import com.lavegue.model.BisType;
import com.lavegue.model.Comment;
import com.lavegue.model.Company;
import com.lavegue.model.ManageUserInfo;

import com.lavegue.common.Constants;
import com.lavegue.model.UserInfo;
import com.lavegue.service.BisGroupService;
import com.lavegue.service.BisTypeService;
import com.lavegue.service.CommentService;
import com.lavegue.service.ManageUserInfoService;
import com.lavegue.service.UserInfoService;
import com.lavegue.util.StringUtil;
import com.lavegue.util.VerifyCodeImg;
import com.opensymphony.xwork2.ActionContext;

/**
 * author: luoweibin
 * date:2011-12-20
 * description:
 *
 */
public class LoginAction extends BaseAction {
	//管理平台的session 中的user对象的key
	public static final String MANAGE_SESSION_USER = "manageUserObject";
	//登录账户
	private String account;
	//密码
	private String password;
	private String oldPassword;
	//验证码
	private String verifyCode;
	
	private List<Comment> whyComments;
	private List<Comment> userComments;
	private List<Comment> baodaoComments;
	private List<BisType> bisTypes;
	
	private BisTypeService bisTypeService;
	private BisGroupService bisGroupService;
	private CommentService commentService;
	
	private UserInfoService userInfoService;
	private ManageUserInfoService manageUserInfoService;
	private List<BisType> listType;
	
	
	public String toLoginPage() throws Exception {
		listType=bisTypeService.findAll();
		bisTypes = bisTypeService.findAll();
		for (BisType t : bisTypes) {
			if (t.getBisGroups() != null) {
				BisGroup[] gps = new BisGroup[t.getBisGroups().size()];
				t.getBisGroups().toArray(gps);
				Arrays.sort(gps);
				t.setBisGroupList(Arrays.asList(gps));
			}
		}
		//1 是vip
		if(this.getSessionUser() != null && this.getSessionUser().getType() == 1) {
			List<BisType> typesTmp = new ArrayList<BisType>();
			for (BisType bt : bisTypes) {
				List<BisGroup> groups = bt.getBisGroupList();
				List<BisGroup> groups2 = new ArrayList<BisGroup>();
				if (this.getSessionUser().getType() == 0 && bt.getUseUsertype() ==2) {
					continue;
				}
				for (BisGroup gp : groups) {
					if (gp.getCompany() == null 
							|| gp.getCompany().getCompanyName().equals(this.getSessionUser().getCompany().getCompanyName()))
					{
						groups2.add(gp);
					}
				}
				bt.setBisGroupList(groups2);
				typesTmp.add(bt);
			}
			bisTypes = typesTmp;
		}
		whyComments = commentService.findWhys();
		userComments = commentService.findUserComment();
		baodaoComments = commentService.findBaodaos(); 
		return "login";
	}
	
	/**
	 * 单独的登录页面
	 * @return
	 * @throws Exception
	 */
	public String loginMain()  throws Exception  {
		login();
		if (this.hasErrors()) {
			toLoginPage();
			return "login";
		}
		toLoginPage();
		return "loginSuccess";
	}
	
	/**
	 * 登录到后台
	 * @return
	 */
	public String loginManage()  throws Exception  {
		System.out.println("login 后台........");
		String loginpage = "manageLogin";
		if (StringUtil.isEmpty(account)) {
			this.addFieldError("account", "请输入账户");
		}
		if (StringUtil.isEmpty(password)) {
			this.addFieldError("password", "请输入密码");
		}
		if(this.hasErrors()) {
			return loginpage;
		}
		
		//根据用户名从数据库中查询出user对象
		ManageUserInfo user = manageUserInfoService.findUserInfoByAccount(account);
		if (user == null) {
			this.addActionError("请输入正确的账户和密码");
			//返回到首页
			return loginpage;
		}
		//比较密码
		if (!user.getPassword().equals(this.password)) {
			this.addActionError("请输入正确的账户和密码");
			return loginpage;
		}
		this.setSessionAtrribute(MANAGE_SESSION_USER, user);
		//跳转到登录后的首页
		return "manageMain";
	}
	
	public String toModifyManagePwd() {
		return "modifyManagePwd";
	}
	
	public String modifyManagePwd() {
		ManageUserInfo u = (ManageUserInfo) this.getSessionAtrribute(Constants.MAN_SESSION_USER);
		if(null== u)
	   {
		   return "lg";
	   }
		if (!u.getPassword().equals(oldPassword)) {
			this.addActionError("旧密码有误,请重新输入");
			return toModifyManagePwd();
		}
		if (password == null || password.equals("")) {
			this.addActionError("请输入新密码");
		} else {
			u.setPassword(password);
			manageUserInfoService.update(u);
			this.addActionMessage("修改成功"); 
		}
		return toModifyManagePwd();
	}
	
	/**
	 * 退出
	 * @return
	 * @throws Exception
	 */
	public String exit()  throws Exception{
		this.removeSession(Constants.SESSION_USER);
		return toLoginPage();
	}
	
	
	public String exitManage() throws Exception {
		this.removeSession(MANAGE_SESSION_USER);
		return "manageLogin";
	}
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	/**
	 * 主页面的登录
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		if (this.getSessionUser() != null) {
			toLoginPage();
			return "loginSuccess";
		}
		System.out.println("login........");
		if (this.getSessionAtrribute(VerifyCodeImg.VALID_CODE_KEY) == null) {
			return toLoginPage();
		}
		if (StringUtil.isEmpty(account)) {
			this.addFieldError("account", "请输入账户");
		}
		if (StringUtil.isEmpty(password)) {
			this.addFieldError("password", "请输入密码");
		}
		if (StringUtil.isEmpty(verifyCode)) {
			this.addFieldError("verifyCode", "请输入验证码");
		}
		if(this.hasErrors()) {
			return toLoginPage();
		}
		
		String code = (String) this.getSessionAtrribute(VerifyCodeImg.VALID_CODE_KEY);
		//检查验证码
		if (!code.equals(verifyCode)) {
			toLoginPage();
			this.addFieldError("verifyCode", "请输入正确的验证码");
			return toLoginPage();
		}
		
		//根据用户名从数据库中查询出user对象
		UserInfo user = userInfoService.findUserInfoByAccount(account);
		if (user == null || (user.getFlagdel() !=null && user.getFlagdel() == 1)) {
			toLoginPage();
			this.addActionError("请输入正确的账户和密码");
			//返回到首页
			return toLoginPage();
		}
		//比较密码
		if (!user.getPassword().equals(this.password)) {
			toLoginPage();
			this.addActionError("请输入正确的账户和密码");
			return toLoginPage();
		}
		this.setSessionAtrribute(Constants.SESSION_USER, user);
		toLoginPage();
		
		
		//跳转到登录后的首页
		return "loginSuccess";
	}

	//get set 方法区
	
	
	public String getAccount() {
		return account;
	}
	public ManageUserInfoService getManageUserInfoService() {
		return manageUserInfoService;
	}

	public void setManageUserInfoService(ManageUserInfoService manageUserInfoService) {
		this.manageUserInfoService = manageUserInfoService;
	}

	public List<BisType> getBisTypes() {
		return bisTypes;
	}

	public List<BisType> getListType() {
		return listType;
	}

	public void setListType(List<BisType> listType) {
		this.listType = listType;
	}

	public void setBisTypes(List<BisType> bisTypes) {
		this.bisTypes = bisTypes;
	}

	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public List<Comment> getWhyComments() {
		return whyComments;
	}

	public void setWhyComments(List<Comment> whyComments) {
		this.whyComments = whyComments;
	}

	public List<Comment> getUserComments() {
		return userComments;
	}

	public void setUserComments(List<Comment> userComments) {
		this.userComments = userComments;
	}

	public List<Comment> getBaodaoComments() {
		return baodaoComments;
	}

	public void setBaodaoComments(List<Comment> baodaoComments) {
		this.baodaoComments = baodaoComments;
	}

	public BisTypeService getBisTypeService() {
		return bisTypeService;
	}

	public void setBisTypeService(BisTypeService bisTypeService) {
		this.bisTypeService = bisTypeService;
	}

	public BisGroupService getBisGroupService() {
		return bisGroupService;
	}

	public void setBisGroupService(BisGroupService bisGroupService) {
		this.bisGroupService = bisGroupService;
	}

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	
}

